/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package ru.papergames.battleserver.network.clientpackets.flashuser;

import org.apache.log4j.Logger;
import org.mmocore.network.ReceivablePacket;
import ru.papergames.Config;
import ru.papergames.battleserver.network.PaperGameClient;
import ru.papergames.battleserver.network.serverpackets.flashuser.ActionFailed;
import ru.papergames.battleserver.network.serverpackets.flashuser.PaperGameServerPacket;

/**
 * Packets received by the game server from clients
 *
 * @author KenM
 */
public abstract class PaperGameClientPacket extends ReceivablePacket<PaperGameClient> implements PaperGameClientPacketTypes {
    protected static final Logger logger = Logger.getLogger(PaperGameClientPacket.class.getName());

    @Override
    protected boolean read() {
        try {
            readImpl();
            return true;
        }
        catch (Exception e) {
            logger.fatal("Client: " + getClient().toString() + " - Failed reading: " + getType() + " - L2J Server Version: " + Config.SERVER_VERSION + " - DP Revision: " + Config.DATAPACK_VERSION);
            e.printStackTrace();
        }
        return false;
    }

    protected abstract void readImpl();

    @Override
    public void run() {
        try {
            // flood protection
//            if (GameTimeController.getGameTicks() - getClient().packetsSentStartTick > 10) {
            long curTime = System.currentTimeMillis();
            if (curTime - getClient().packetsSentStartTick > 1000) {
//                getClient().packetsSentStartTick = GameTimeController.getGameTicks();
                getClient().packetsSentStartTick = curTime;
                getClient().packetsSentInSec = 0;
            } else {
                getClient().packetsSentInSec++;
                if (getClient().packetsSentInSec > 12) {
                    if (getClient().packetsSentInSec < 100) {
                        sendPacket(ActionFailed.STATIC_PACKET);
                    }
                    return;
                }
            }

            runImpl();

            /* Removes onspawn protection - player has faster computer than average
                *
                * True for these packets:
                * AttackRequest
                * MoveBackwardToLocation
                * RequestActionUse
                * RequestMagicSkillUse
                *
                * it could include pickup and talk too, but less is better
                */
            /*
            if (triggersOnActionRequest() && getClient().getActiveChar() != null)
            	getClient().getActiveChar().onActionRequest();
            */
        }
        catch (Throwable t) {
            logger.fatal("Client: " + getClient().toString() + " - Failed running: " + getType() + " - L2J Server Version: " + Config.SERVER_VERSION + " - DP Revision: " + Config.DATAPACK_VERSION);
            t.printStackTrace();
        }
    }

    protected abstract void runImpl();

    protected final void sendPacket(PaperGameServerPacket gsp) {
        getClient().sendPacket(gsp);
    }

    /**
     * @return A String with this packet name for debuging purposes
     */
    public abstract String getType();

    /**
     * Overriden with true value on some packets that should disable spawn protection
     */
    protected boolean triggersOnActionRequest() {
        return false;
    }
}
